
import WxValidate from '../../../asstes/WxValidate.js'
var utils = require('../../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    date: '',
    time: '',
    appointmentDate: '',
    showModal: false,
    formValues: {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.initValidate()
    var now = new Date()
    var result = utils.formatTime(now)
    this.setData({
      date: result.date,
      time: result.time
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  // 验证必填字段
  initValidate() {
    // 验证字段的规则
    const rules = {
      number_diners: {
        required: true,
        number: true
      },
      appointmentDateName: {
        required: true
      },
      appointmentTel: {
        required: true,
        tel: true,
      }
    }

    // 验证字段的提示信息，若不传则调用默认的信息
    const messages = {
      number_diners: {
        required: '请输入用餐人数',
        number: '请输入正确的数字',
      },
      appointmentDateName: {
        required: '请输入联系人姓名'
      },
      appointmentTel: {
        required: '请输入手机号',
        tel: '请输入正确的手机号',
      }
    }

    // 创建实例对象
    this.WxValidate = new WxValidate(rules, messages)
  },
  bindDateChange: function (e) {
    // console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      date: e.detail.value,
      appointmentDate: e.detail.value
    })
  },
  bindTimeChange: function (e) {
    // console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      time: e.detail.value,
      appointmentTime: e.detail.value
    })
  },
  // 提交支付信息
  bindPayment() {
    /**
     * 支付接口
     * 支付成功，跳转到成功界面
     */
    this.selectComponent('#payModal').fadeOut()
    const that = this
    wx.showLoading()
    setTimeout(function () {
      wx.hideLoading()
      that.setData({
        showModal: false
      })
      wx.redirectTo(
        {
          url: '../result/result'
        }
      )
    }, 1000)
  },
  //取消支付
  cancelPay: function () {
    this.setData({
      showModal: false
    })
  },
  formSubmit: function (e) {
    const params = e.detail.value
    const that = this
    that.setData({
      showModal: true,
      formValues: params
    })
    // 验证必填字段 
    // if (!this.WxValidate.checkForm(params)) {
    //  const error = this.WxValidate.errorList[0]
    //   wx.showModal({
    //     content: error.msg,
    //     showCancel: false
    //   })
    //  return false
    // }else{
    //   that.setData({
    //     showModal: true,
    //     formValues: params
    //   })
    // }
  }
})